﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    public static GameManager Instance;
    private QuestionPool currentQuestionPool;
    private int currentIndex;
    private GameState gameState;
    private int[] randomQuestionIndex;
    private const int gameQuestionNumber = 10;
    private void Awake()
    {
        Instance = this;
    }

    public Question GetCurrentQuestion()
    {
        return currentQuestionPool.questions[randomQuestionIndex[currentIndex]];
    }

    public int GetCurrentQuestionIndex()
    {
        return currentIndex;
    }


    private int wrongNumber;
    /// <summary>
    /// 评级和错误数量挂钩
    /// </summary>
    /// <returns></returns>
    public int GetEvaluateLevel()
    {
        return wrongNumber;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="option"></param>
    /// <returns></returns>
    public bool SubmitAnswer(int option)
    {
        bool result;
        if (option == GetCurrentQuestion().answer)
        {
            result = true;
        }
        else
        {
            result = false;
            wrongNumber++;
        }
        if (wrongNumber >= 3)
        {
            gameState = GameState.Fail;
        }
        else
        {
            currentIndex++;
            if(currentIndex >= randomQuestionIndex.Length || currentIndex >= gameQuestionNumber)
            {
                gameState = GameState.Success;
            }
        }
        return result;
    }

    public void StartGame(int choose)
    {
        
        currentQuestionPool = ResourceManager.Instance.GetQuestionPool(choose);
        
        InitGameData();
    }

    public void RestartGame()
    {
        InitGameData();
    }


    
    /// <summary>
    /// 游戏进度数据
    /// </summary>
    private void InitGameData()
    {
        randomQuestionIndex = NormalTool.GetRandomIntArray(gameQuestionNumber, 0, currentQuestionPool.questions.Count - 1);
        gameState = GameState.Playing;
        currentIndex = 0;
        wrongNumber = 0;
    }

    public GameState GetGameState()
    {
        return gameState;
    }
}

public enum GameState
{
    Playing,
    Success,
    Fail
}
